﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="PageTitle" runat="server">首页</asp:Content>
<asp:Content ID="indexStyle" ContentPlaceHolderID="StyleContent" runat="server">
    <style type="text/css">
        .list-group-item ul{
            margin-top:10px;
            margin-bottom:0px;
        }
    </style>
</asp:Content>
<asp:Content ID="indexViewTitle" ContentPlaceHolderID="ViewTitle" runat="server">首页</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="ViewContent" runat="server">
    <div style="height:100%;overflow:hidden;overflow-y:auto;">
        <ul class="list-group" style="color:black;">
            <%
                var arrMonths = (string[])ViewData["MonthRange"];
                for (int i = 0; i < arrMonths.Length; i++){
            %>
            <li class="list-group-item" id="_month_<%:i %>" title="<%:arrMonths[i] %>" onclick='groupClick(this,<%="\"" + arrMonths[i] + "\"" %>,true);'><span class="badge" style="cursor:pointer;"><i class="cus-icon-add"></i>&nbsp;</span>
                <b style="cursor:pointer;">&nbsp;&nbsp;&nbsp;&nbsp;<%=arrMonths[i] %></b>
                <ul class="list-group hidden" style="color:black">
                    <li class="list-group-item">正在加载...</li>
                </ul>
            </li>
            <%} %>
        </ul>
    </div>
</asp:Content>
<asp:Content ID="indexFooter" ContentPlaceHolderID="ViewFooter" runat="server">
    <div class="row">
        <div class="col-xs-12 text-left">
            <a class="btn btn-success" href='<%:Url.Action("Edit","Note") %>'><i class="cus-icon-pencil"></i>写日志</a>
        </div>
    </div>
</asp:Content>
<asp:Content ContentPlaceHolderID="ScriptContent" runat="server">
    <script type="text/javascript">
        function groupClick(sender, group, allowExpand) {
            var jQueryObj = $(sender);
            if (!sender.isLoaded) loadMonthNotes(sender, group, allowExpand);
            sender.isLoaded = true;
            jQueryObj.children("ul").each(function () {
                if (this.className.indexOf("hidden") > 0) $(this).removeClass("hidden");
                else $(this).addClass("hidden");
            })
        }
        function loadMonthNotes(sender, month, allowExpand) {
            asyncRequest('<%:Url.Action("MonthNotes","Note") %>', { month: month }, function (result) {
                var container = $(sender).children("ul");
                if (!!result.data && Object.prototype.toString.call(result.data) === '[object Array]') {
                    if (result.data.length > 0) {
                        for (var i = 0; i < result.data.length; i++) {
                            var element = null;
                            if (i == 0) element = container.children("li").css("cursor", "pointer").data("date", result.data[i].NoteDate).text(result.data[i].Description);
                            else element = $("<li>").addClass("list-group-item").css("cursor", "pointer").data("date", result.data[i].NoteDate).text(result.data[i].Description).appendTo(container);
                            if (allowExpand) {
                                element.click(function (e) {
                                    groupClick(this, $(this).data("date"));
                                    return false;
                                });
                                $("<ul>").addClass("list-group").addClass("hidden").append($("<li>").addClass("list-group-item").text("正在加载...")).appendTo(element);
                            }
                            else {
                                var noteTitle = element.text();
                                element.html("<div class='row'><div class='col-xs-9 text-left'><a href='<%:Url.Action("Edit", "Note")%>/" + result.data[i].NoteID + "'>" + noteTitle + "</a></div><div class='col-xs-3 text-right'><a href='javascript:void(0);' onclick='delNote(this," + result.data[i].NoteID + ")'>[删除]</a></div></div>")
                                element.click(function (e) {
                                    e.stopPropagation();
                                });
                            }
                        }
                        return;
                    }
                }
                var child = container.children("li");
                child.text("<无数据>");
                child.click(function (e) { return false; });
            }, function (result) {
                var child = $(sender).children("ul").children("li");
                child.text(result.message);
                child.click(function (e) { return false; });
            });
        }
        function delNote(sender, noteid) {
            request('<%:Url.Action("Delete","Note") %>', { id: noteid, date: $(sender).parents("li[id^='_month_']").attr("title") || "" }, function (result) {
                $(sender).parents("li:first").each(function () {
                    if (this.parentNode.childNodes.length > 1) this.removeNode(true);
                    else $(this).text("<无数据>");
                });
            }, function (result) {
                $(sender).popover({
                    container: "body",
                    html: true,
                    delay: {"show": 2000 },
                    placement: "bottom",
                    content: "<span style='color:red;'>" + result.message + "</span>"
                });
                $(sender).popover('show');
                var thandler = setTimeout(function () { $(sender).popover('hide'); clearTimeout(thandler); }, 2000);
            });
        }
    </script>
</asp:Content>
